using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;
using System.Linq;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OAuth;
using ezCloud.Api.Models;
using ezCloud.Api.Providers;
using ezCloud.Api.Results;
using ezCloud.Data;
using System.Configuration;
using System.Net;
using ezCloud.Api.Services;
using Microsoft.Owin.Testing;
using System.Web.Script.Serialization;
using System.IO;
using Microsoft.Owin.Security.Infrastructure;
using ezCloud.Api.Common;
using Newtonsoft.Json;
using System.Web.Configuration;

namespace ezCloud.Api.Controllers
{
    //[Authorize]
    [RoutePrefix("api/Email")]

    public class EmailController : ezController
    {
        private const string LocalLoginProvider = "Local";
        private ApplicationUserManager _userManager;

        public EmailController()
        {
        }
      
        // GET api/Email/SendReferralCode
        //[HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
       
        [AllowAnonymous]
        [Route("SendReferralCode")]
        [HttpGet]
        public async Task<IHttpActionResult> ReferralCode(int HotelId)
        {
            if (!InternalServices.isAllowed()) { return BadRequest("IP_NOT_ALLOW_EXIST:" + HttpContext.Current.Request.UserHostAddress); };
            
            if (ReferralProgramUltility.CreateHotelReferralCode(HotelId) == true)
            {
                var hotelOwner = context.Hotels.Where(m => m.HotelId == HotelId).Single();
                var userOwner = context.AspNetUsers.Where(o => o.Id == hotelOwner.HotelOwnerId).FirstOrDefault();

                var hotelReferralCode = ReferralProgramUltility.ReferralCodeGenerator(HotelId);
                if (hotelReferralCode != null)
                {
                    EmailSender.SendEmailReferralCode(userOwner.Email, userOwner.Email, hotelReferralCode, hotelOwner.HotelName, hotelOwner.HotelId);
                }
                
            }

            

            return Ok();
        }

        [AllowAnonymous]
        [Route("SendEmail")]
        [HttpGet]
        public async Task<IHttpActionResult> SendEmail(string email, string subject, string content )
        {
            if (!InternalServices.isAllowed()) { return BadRequest("IP_NOT_ALLOW_EXIST:" + HttpContext.Current.Request.UserHostAddress); };
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            subject = subject + "[" + PmsUrl + "]";
            EmailSender.SendEmail(email, subject, content);
            
            return Ok();
        }

    }

}
